﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;

public partial class Manage_Member_Item : System.Web.UI.Page
{
    protected KeRui.BLL.Member bllMember;
    protected KeRui.Model.Member modelMember = new KeRui.Model.Member();
    public string Act = ManageHelper.PageAct();
    private ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();//登陆判断
        bllMember = new KeRui.BLL.Member();
        switch (Act)
        {
            case "addsave":
                MemberAdd();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;            
            case "state":
                State();
                break;
            default:
                break;
        }
    }


    #region 添加
    private void MemberAdd()
    {
        ManageHelper.CheckAdminPower("system_memberadd");//权限
        modelMember.Username = PageRequest.GetString("Username");
        if (modelMember.Username == "")
        {
            nsb.AddErr("请输入用户名。");
        }
        string Password = Utils.MD5(PageRequest.GetString("Password"));
        if (Password == "")
        {
            nsb.AddErr("请输入密码。");
        }
        string chkPassword = Utils.MD5(PageRequest.GetString("chkPassword"));
        if (chkPassword == "")
        {
            nsb.AddErr("请输入确认密码。");
        }
        modelMember.GroupID = PageRequest.GetInt("GroupID", -1);
        if (modelMember.GroupID == -1)
        {
            modelMember.GroupID = 0;
        }
        modelMember.State = PageRequest.GetInt("State",0);
        modelMember.CreateIP = Utils.GetRealIP();
        nsb.ChkErr();
        int RetFlag = bllMember.Add(modelMember,Password);
        if (RetFlag == 1)
        {
            nsb.AddErr("对不起！已存在相同的用户名。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_memberadd", "添加了“"+modelMember.Username+"”用户。");
            Response.Redirect("Item.aspx?sucmsg=添加成功。");
        }
    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {
        ActFlag = "editsave";
        ActName = "修改";
        int UserID = PageRequest.GetInt("ID", -1);
        if (UserID == -1)
        {
            nsb.AddErr("Error:不存在用户id。");
            nsb.ChkErr();
        }
        modelMember = bllMember.GetModel(UserID);
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("system_memberedit");//权限
        modelMember.UserID = PageRequest.GetInt("UserID", -1);
        modelMember.Username = Utils.ChkSQL(PageRequest.GetString("Username"));
        if (modelMember.Username == "")
        {
            nsb.AddErr("请输入用户名。");
        }
        //string Password = Utils.ChkSQL(PageRequest.GetString("Password"));
        //if (Password == "")
        //{
        //    nsb.AddErr("请输入密码。");
        //}
        //string chkPassword = Utils.ChkSQL(PageRequest.GetString("chkPassword"));
        //if (chkPassword == "")
        //{
        //    nsb.AddErr("请输入确认密码。");
        //}
        modelMember.GroupID = PageRequest.GetInt("GroupID", 0);
        if (modelMember.GroupID == 0)
        {
            nsb.AddErr("请选择用户组。");
        }
        modelMember.State = PageRequest.GetInt("State", -1);
        if (modelMember.State == -1)
        {
            nsb.AddErr("Error：state。"); nsb.ChkErr();
        }
        nsb.ChkErr();
        int RetFlag = bllMember.Update(modelMember);
        if (RetFlag == 1)
        {
            nsb.AddErr("对不起！已存在相同的用户名。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_memberedit", "修改了“" + modelMember.Username + "”用户。");
            Response.Redirect("Item.aspx?act=edit&ID=" + modelMember.UserID + "&sucmsg=修改成功。");
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        ManageHelper.CheckAdminPower("system_memberdelete");//权限
        int UserID = PageRequest.GetInt("ID", -1);
        if (UserID == -1)
        {
            nsb.AddErr("该站点不存在。");
            nsb.ChkErr();
        }
        modelMember = bllMember.GetModel(UserID);
        if (modelMember == null)
        {
            nsb.AddErr("对不起！您要删除的信息不存在。"); nsb.ChkErr();
        }
        bllMember.Delete(UserID);
        ManageHelper.AddLog("system_memberedit", "删除了“" + modelMember.Username + "”用户。");
        Response.Redirect("List.aspx?sucmsg=恭喜。删除成功。");
    }
    #endregion

    #region 解锁和锁定
    private void State()
    {
        int UserID = PageRequest.GetInt("ID", -1);
        int st = PageRequest.GetInt("st", 0);
        if (UserID == -1)
        {
            nsb.AddErr("对不起。你想要操作的用户不存在。");
            nsb.ChkErr();
        }
       int RetInt = bllMember.ChageState(UserID,-1);
       if (st == 1)
       {
           Response.Redirect("List.aspx?sucmsg=恭喜。锁定成功。");
       }
       else
       {
           Response.Redirect("List.aspx?sucmsg=恭喜。解锁成功。");
       }
    }
    #endregion
}
